<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-col :span="24">
      <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
        <el-col :span="24">
          <el-form-item :label="$t('msgDingtalkTemplate.templateId')" prop="templateId">
            <!--文本框-->
            <el-input v-model="dataForm.templateId" :placeholder="$t('msgDingtalkTemplate.templateId')" clearable>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('msgDingtalkTemplate.templateName')" prop="templateName">
            <!--文本框-->
            <el-input v-model="dataForm.templateName" :placeholder="$t('msgDingtalkTemplate.templateName')" clearable>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('msgDingtalkTemplate.msgType')" prop="msgType">
            <!--文本框-->
            <el-select v-model="dataForm.msgType" :placeholder="$t('msgDingtalkTemplate.msgType')" @change="typeChange(dataForm.msgType)" clearable>
              <el-option v-for="template in $getDictList('DINGTALK_MSG_TYPE', true)" :key="template.val" :label="template.label" :value="template.val"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('msgDingtalkTemplate.content')" prop="content">
            <!--文本域-->
            <el-input type="textarea" :rows="3" v-model="dataForm.content" :placeholder="$t('msgDingtalkTemplate.content')"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('common.remark')" prop="remark">
            <!--文本框-->
            <el-input v-model="dataForm.remark" :placeholder="$t('common.remark')" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-form>
    </el-col>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" :loading="loading" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      loading: false,
      visible: false,
      appList: [],
      dingTalkAccountList: [],
      dataForm: {
        id: '',
        appId: 'EDP',
        signatureCode: '',
        templateId: '',
        templateName: '',
        content: '',
        remark: '',
        accountId: '',
        creator: '',
        createDate: '',
        updater: '',
        updateDate: '',
        msgType: ''
      },
      msgType: {
        text: '{"msgtype": "text","text": {"content": "张三的请假申请"}}',
        image: '{"msgtype": "image","image": {"media_id": "@lALPDeC2u1ElprDNAZDNAlg"}}',
        voice: '{"msgtype": "voice","voice": {"media_id": "MEDIA_ID","duration": "10"}}',
        file: '{"msgtype": "file","file": {"media_id": "@lA7PDeC2u1E2--POHffTus4G2q6O"}}',
        link: '{"msgtype": "link","link": {"messageUrl": "http://uniedp.cn/","picUrl":"@lALOACZwe2Rk","title": "测试","text": "测试"}}',
        markdown: '{"msgtype": "markdown","markdown": {"title": "首屏会话透出的展示内容","text": "消息内容"}}',
        action_card: '{"msgtype": "action_card","action_card": {"title": "是透出到会话列表和通知的文案","markdown": "支持markdown格式的正文内容","single_title": "查看详情","single_url": "http://uniedp.cn/"}}'
      }
    }
  },
  computed: {
    dataRule () {
      return {
        templateId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }

        ],
        templateName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }

        ],
        msgType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      // this.getAppList()
      // this.getdingTalkAccountList()
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取APP
    // getAppList () {
    //   return this.$http.get('/sys/sysApp/list').then(({ data: res }) => {
    //     if (res.code !== 0) {
    //       return this.$message.error(res.msg)
    //     }
    //     this.appList = res.data
    //   }).catch(() => { })
    // },
    // 获取钉钉账号
    // getdingTalkAccountList () {
    //   return this.$http.get('/msg/msgDingtalkAccount/list').then(({ data: res }) => {
    //     if (res.code !== 0) {
    //       return this.$message.error(res.msg)
    //     }
    //     this.dingTalkAccountList = res.data
    //     console.log(this.dingTalkAccountList)
    //   }).catch(() => { })
    // },
    typeChange (type) {
      if (type === 'text') {
        this.dataForm.content = this.msgType.text
      } else if (type === 'image') {
        this.dataForm.content = this.msgType.image
      } else if (type === 'file') {
        this.dataForm.content = this.msgType.file
      } else if (type === 'markdown') {
        this.dataForm.content = this.msgType.markdown
      } else if (type === 'link') {
        this.dataForm.content = this.msgType.link
      } else if (type === 'voice') {
        this.dataForm.content = this.msgType.voice
      } else if (type === 'action_card') {
        this.dataForm.content = this.msgType.action_card
      }
    },
    // 获取信息
    getInfo () {
      this.loading = true
      this.$http.get(`/msg/msgDingtalkTemplate/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => { })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.loading = true
        this.$http[!this.dataForm.id ? 'post' : 'put']('/msg/msgDingtalkTemplate/', this.dataForm).then(({ data: res }) => {
          this.loading = false
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
